function [reversalVals,idx] = getLastNReversals(x, N, isInLeadIn)
% x should be a binary vector [0 1]...


    changes = sign(diff(x));
    
    % but only a changes that reverse the direction are reversals
    reversals = zeros(size(x));
    lastChange = 0;
    for i = 1:length(changes)
        if changes(i) ~= 0 && changes(i) ~= lastChange
            reversals(i) = 1;
            lastChange = changes(i);
        end
    end
    
    % remove all but last N indices
    tmp = find(reversals);
    idx = zeros(size(x));
    idx(tmp(end-(N-1):end)) = 1;
    idx = (idx == 1);
    
    % get values
    reversalVals = x(idx);
    
end